package com.mmall.filter;

import com.mmall.common.RequestHolde;
import com.mmall.model.SysUser;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class LoginFilter implements Filter{


    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        SysUser sysUser = (SysUser) req.getSession().getAttribute("user");
          if (sysUser == null){

              String path = "/signin.jsp";
              resp.sendRedirect(path);
              return;
          }
          RequestHolde.add(sysUser);
          RequestHolde.add(req);
          filterChain.doFilter(servletRequest,servletResponse);
          return;

    }

    public void destroy() {

    }
}
























